package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.vo.StatisVO;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.member.vo.UserVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * spring定时器
 * 用于将redis中的数据持久化道mysql中
 */
@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Scheduled(cron = "0/10 * * * * ? ")
    public void testWork(){
        System.out.println("------------------------------------- vo的持久化 start -----------------------------------------------------");
        //查询出所有的vo
        List<StrategyStatisVO> vos =
            strategyStatisVOService.listVo(RedisKeys.STRATEGY_STATIS_VO.join("*"));

        for (StrategyStatisVO vo : vos) {

            StatisVO statisVO = new StatisVO();
            BeanUtils.copyProperties(vo,statisVO);
            //持久化的mysql中
            strategyDetailService.updateStatisVo(statisVO);
        }

        //黑名单的持久化
        List<UserInfo> userInfos = userInfoService.list();
        if(userInfos != null && userInfos.size()>0){
            for (UserInfo userInfo : userInfos) {
                List<UserVO> userVOS = userInfoRedisService.blackUserList(userInfo.getId());
                if(userVOS != null && userVOS.size()>0){
                    for (UserVO userVO : userVOS) {
                        userInfoService.persistenceBlackUser(userInfo.getId(),userVO.getId());
                    }
                }
            }
        }

        System.out.println("------------------------------------- vo的持久化 end -------------------------------------------------------");
    }
}
